home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 10 - Networking / NovelNetwar / dialogs.c < prev    next >
Text File  |  1995-05-12  |  9KB  |  484 lines

  1. //    This  module takes care of dialog-box related stuff.
  2.  
  3. #include "NovelNetwar.h"
  4.  
  5. #include <GestaltEqu.h>
  6. #include <Traps.h>
  7. #include <CTBUtilities.h>
  8. #include <EPPC.h>
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. void FatalError(char *fatalString)
  17. {
  18.     ErrorAlert(fatalString);
  19.     
  20.     DoShutDown();
  21.     
  22.     ExitToShell();
  23. }
  24.  
  25.  
  26.  
  27. void ErrorAlert(char *p0)
  28. {
  29. GrafPtr            oldPort;
  30. DialogPtr        theDPtr,tempDPtr;
  31. short            itemHit;
  32. short            type;
  33. Handle            theItem;
  34. Rect            theRect;
  35. EventRecord        theEvent;
  36.  
  37.     GetPort(&oldPort);
  38.     
  39.     InitCursor();
  40.     
  41.     if (!(theDPtr = GetNewDialog(ERRORDLOG, nil,(WindowPtr) -1L)))
  42.     {
  43.         SysBeep(1);
  44.     }
  45.     
  46.     SetPort(theDPtr);
  47.     
  48.     CenterWindow(theDPtr);
  49.     ShowWindow(theDPtr);
  50.     
  51.     ((DialogPeek) theDPtr)->aDefItem = 1;
  52.     
  53.     GetDItem(theDPtr, 2, &type, &theItem, &theRect);
  54.     SetDItem(theDPtr, 2, type, (Handle) HiliteDefaultButton, &theRect);
  55.     
  56.     GetDItem(theDPtr, 4, &type, &theItem, &theRect);
  57.     CtoPstr(p0);
  58.     SetIText(theItem,(unsigned char *) p0);
  59.     PtoCstr((unsigned char *) p0);
  60.     
  61.     if (GetNextEvent(updateMask,&theEvent))
  62.     {
  63.         if (theEvent.what == kHighLevelEvent)
  64.         {
  65.             DoHighLevel(&theEvent);
  66.         }
  67.  
  68.         else if (theEvent.message == (long) theDPtr)
  69.         {
  70.             DialogSelect(&theEvent,&tempDPtr,&itemHit);
  71.         }
  72.     }
  73.     
  74.     SysBeep(1);
  75.     
  76.     do 
  77.     {
  78.         ModalDialog((ModalFilterProcPtr) MyFilter,&itemHit);
  79.         
  80.     } while (itemHit != 1);
  81.     
  82.     DisposDialog(theDPtr);
  83.  
  84.     SetPort(oldPort);
  85. }
  86.  
  87.  
  88.  
  89. //    This is my custom dialog event preprocessor.  It does handy things like intercept the escape
  90. //    key and translate it into a click on a Cancel button (if there is one), translate command-key
  91. //    events into mouseclicks on buttons, etc.
  92.  
  93. OSErr ProcessDialogCmdKey(DialogPtr theDialog,EventRecord *theEvent,short *itemHit)
  94. {
  95. char        ch,tempString[256];
  96. int            i,j,length,numItems;
  97. Handle        itemHandle;
  98. Rect        dispRect;
  99. long        theTicks;
  100. long        hasDITLExtensions;
  101. short        itemType;
  102. int            theResult;
  103.  
  104.  
  105.     theResult = false;
  106.     
  107.     if (theEvent->what == keyDown || theEvent->what == autoKey)
  108.     {
  109.         ch = theEvent->message & charCodeMask;
  110.         
  111.         if (ch >= 'a' && ch <= 'z')
  112.             ch = 'A' + ch - 'a';
  113.         
  114.         
  115.         if (((DialogPeek) theDialog)->aDefItem > 0 && (ch == '\r' || ch == '\n' || ch == 0x03))
  116.         {
  117.             GetDItem(theDialog,((DialogPeek) theDialog)->aDefItem,&itemType,&itemHandle,&dispRect);
  118.             
  119.             if ((**(ControlHandle) itemHandle).contrlHilite != 255)
  120.             {
  121.                 HiliteControl((ControlHandle) itemHandle,inButton);
  122.                 
  123.                 theTicks = TickCount();
  124.                 
  125.                 while (TickCount() < theTicks + 15);
  126.                 
  127.                 HiliteControl((ControlHandle) itemHandle,0);
  128.                 
  129.                 *itemHit = ((DialogPeek) theDialog)->aDefItem;
  130.                 
  131.                 theResult = true;
  132.                 
  133.                 goto EXITPOINT;
  134.             }
  135.         }
  136.         
  137.         if ((theEvent->modifiers & cmdKey) && ((DialogPeek) theDialog)->editField != -1 && ((DialogPeek) theDialog)->textH != 0L)
  138.         {
  139.             switch (ch)
  140.             {
  141.                 case 'C':
  142.                     DlgCopy(theDialog);
  143.                     *itemHit = -1;
  144.                     theResult = true;
  145.                     goto EXITPOINT;
  146.                     break;
  147.                 
  148.                 case 'V':
  149.                     DlgPaste(theDialog);
  150.                     *itemHit = -1;
  151.                     theResult = true;
  152.                     goto EXITPOINT;
  153.                     break;
  154.                 
  155.                 case 'X':
  156.                     DlgCut(theDialog);
  157.                     *itemHit = -1;
  158.                     theResult = true;
  159.                     goto EXITPOINT;
  160.                     break;
  161.             }
  162.         }
  163.         
  164.         if ((theEvent->modifiers & cmdKey) || (((DialogPeek) theDialog)->editField == -1) || (((DialogPeek) theDialog)->textH == 0L) || ch == 0x1b)
  165.         {
  166.             numItems = -1;
  167.             
  168.             if (TrapAvailable((int) _Gestalt) == noErr)
  169.             {
  170.                 if (Gestalt(gestaltDITLExtAttr,&hasDITLExtensions) == noErr)
  171.                 {
  172.                     if (hasDITLExtensions & (1 << gestaltDITLExtPresent))
  173.                     {
  174.                         numItems = CountDITL(theDialog);
  175.                     }
  176.                 }
  177.             }
  178.             
  179.             if (numItems >= 1)
  180.             {
  181.                 for (i=1;i<=numItems;i++)
  182.                 {
  183.                     GetDItem(theDialog,i,&itemType,&itemHandle,&dispRect);
  184.                     
  185.                     if (itemType == ctrlItem + btnCtrl && itemHandle)
  186.                     {
  187.                         GetCTitle((ControlHandle) itemHandle,(unsigned char *) tempString);
  188.                         
  189.                         if (ch == 0x1B || ch == '.')
  190.                         {
  191.                             PtoCstr((unsigned char *) tempString);
  192.                             
  193.                             if (strcmp(tempString,"Cancel") == EQSTR)
  194.                             {
  195.                                 GetDItem(theDialog,i,&itemType,&itemHandle,&dispRect);
  196.                                 
  197.                                 if ((**(ControlHandle) itemHandle).contrlHilite != 255)
  198.                                 {
  199.                                     HiliteControl((ControlHandle) itemHandle,inButton);
  200.                                     
  201.                                     theTicks = TickCount();
  202.                                     
  203.                                     while (TickCount() < theTicks + 15);
  204.                                     
  205.                                     HiliteControl((ControlHandle) itemHandle,0);
  206.                                     
  207.                                     *itemHit = i;
  208.                                     
  209.                                     theResult = true;
  210.                                     goto EXITPOINT;
  211.                                 }
  212.                             }
  213.                         }
  214.                         
  215.                         else
  216.                         {
  217.                             length = tempString[0];
  218.                             
  219.                             for (j=1;j<=length;j++)
  220.                             {
  221.                                 if (tempString[j] >= 'A' && tempString[j] <= 'Z')
  222.                                 {
  223.                                     if (tempString[j] == ch)
  224.                                     {
  225.                                         GetDItem(theDialog,i,&itemType,&itemHandle,&dispRect);
  226.                                         
  227.                                         if ((**(ControlHandle) itemHandle).contrlHilite != 255)
  228.                                         {
  229.                                             HiliteControl((ControlHandle) itemHandle,inButton);
  230.                                             
  231.                                             theTicks = TickCount();
  232.                                             
  233.                                             while (TickCount() < theTicks + 15);
  234.                                             
  235.                                             HiliteControl((ControlHandle) itemHandle,0);
  236.                                             
  237.                                             *itemHit = i;
  238.                                             
  239.                                             theResult = true;
  240.                                             goto EXITPOINT;
  241.                                         }
  242.                                     }
  243.                                     
  244.                                     else
  245.                                         j = length + 1;
  246.                                 }
  247.                             }
  248.                         }
  249.                     }
  250.                 }
  251.                 
  252.                 if (ch != 'X' && ch != 'C' && ch != 'V')
  253.                     SysBeep(1);
  254.             }
  255.         }
  256.     }
  257.  
  258.  
  259. EXITPOINT:
  260.  
  261.     return(theResult);
  262. }
  263.  
  264.  
  265. pascal Boolean MyFilter(DialogPtr theDialog,EventRecord *theEvent,short *itemHit)
  266. {
  267. Boolean        theResult;
  268.  
  269.     theResult = ProcessDialogCmdKey(theDialog,theEvent,itemHit);
  270.     
  271.     return(theResult);
  272. }
  273.  
  274.  
  275.  
  276. void CenterWindow(WindowPtr wPtr)
  277. {
  278. int        screenWidth,screenHeight,windowWidth,windowHeight,left,top;
  279.  
  280.     if (wPtr == 0L)
  281.         return;
  282.     
  283.     screenWidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
  284.     screenHeight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - 40;
  285.     
  286.     windowWidth = wPtr->portRect.right - wPtr->portRect.left;
  287.     windowHeight = wPtr->portRect.bottom - wPtr->portRect.top + 40;
  288.     
  289.     
  290.     left = qd.screenBits.bounds.left + (screenWidth - windowWidth)/2;
  291.     
  292.     top = qd.screenBits.bounds.top + 55 + (screenHeight - windowHeight)/2;
  293.     
  294.     if (left < 0)
  295.         left = 0;
  296.     
  297.     if (top < 55)
  298.         top = 55;
  299.     
  300.     MoveWindow(wPtr,left,top,false);
  301. }
  302.  
  303.  
  304. pascal void HiliteDefaultButton(DialogPtr theDPtr,int whichItem)
  305. {
  306. short        type;
  307. Handle        theItem;
  308. Rect        theRect;
  309. PenState    thePenState;
  310. GrafPtr        oldPort;
  311.  
  312.  
  313.     if (theDPtr)
  314.     {
  315.         GetPort(&oldPort);
  316.         SetPort(theDPtr);
  317.         
  318.         GetPenState(&thePenState);
  319.         
  320.         GetDItem(theDPtr, ((DialogPeek) theDPtr)->aDefItem, &type, &theItem, &theRect);
  321.         PenNormal();
  322.         PenSize(3,3);
  323.         InsetRect(&theRect,-4,-4);
  324.         FrameRoundRect(&theRect,16,16);
  325.         PenSize(1,1);
  326.         
  327.         SetPenState(&thePenState);
  328.         SetPort(oldPort);
  329.     }
  330. }
  331.  
  332.  
  333.  
  334.  
  335.  
  336. OSErr GetInput(char    *thePrompt,char *theText)
  337. {
  338. DialogPtr        theDPtr,tempDPtr;
  339. short            itemHit, type;
  340. Handle            theItem;
  341. Rect            theRect;
  342. GrafPtr            oldPort;
  343. EventRecord        theEvent;
  344.  
  345.  
  346.     GetPort(&oldPort);
  347.     
  348.     InitCursor();
  349.     
  350.     if (!(theDPtr = GetNewDialog(INPUTDLOG, nil,(WindowPtr) -1L)))
  351.     {
  352.         FatalError("Can't allocate memory for window");
  353.     }
  354.     
  355.     SetPort(theDPtr);
  356.     
  357.     CenterWindow(theDPtr);
  358.     ShowWindow(theDPtr);
  359.     
  360.     ((DialogPeek) theDPtr)->aDefItem = 1;
  361.     
  362.     GetDItem(theDPtr, 5, &type, &theItem, &theRect);
  363.     SetDItem(theDPtr, 5, type,(Handle) HiliteDefaultButton, &theRect);
  364.     
  365.     GetDItem(theDPtr, 3, &type, &theItem, &theRect);
  366.     CtoPstr(thePrompt);
  367.     SetIText(theItem,(unsigned char *) thePrompt);
  368.     PtoCstr((unsigned char *) thePrompt);
  369.     
  370.     GetDItem(theDPtr, 4, &type, &theItem, &theRect);
  371.     CtoPstr(theText);
  372.     SetIText(theItem,(unsigned char *) theText);
  373.     SelIText(theDPtr, 4, 0, 256);
  374.     PtoCstr((unsigned char *) theText);
  375.     
  376.     if (GetNextEvent(updateMask,&theEvent))
  377.     {
  378.         if (theEvent.what == kHighLevelEvent)
  379.         {
  380.             DoHighLevel(&theEvent);
  381.         }
  382.  
  383.         else if (theEvent.message == (long) theDPtr)
  384.         {
  385.             DialogSelect(&theEvent,&tempDPtr,&itemHit);
  386.         }
  387.     }
  388.     
  389.     
  390.     do 
  391.     {
  392.         ModalDialog((ModalFilterProcPtr) MyFilter,&itemHit);
  393.         
  394.     } while (itemHit!=1 && itemHit!=2);
  395.     
  396.     
  397.     GetDItem(theDPtr, 4, &type, &theItem, &theRect);
  398.     
  399.     if (itemHit==1)
  400.     {
  401.         GetIText(theItem,(unsigned char *) theText);
  402.         PtoCstr((unsigned char *) theText);
  403.         
  404.         if (theText[0])
  405.             itemHit = noErr;
  406.         
  407.         else
  408.             itemHit = -1;
  409.     }
  410.     else
  411.     {
  412.         theText[0] = 0;
  413.         itemHit = -1;
  414.     }
  415.     
  416.     DisposDialog(theDPtr);
  417.     SetPort(oldPort);
  418.     
  419.     return(itemHit);
  420. }
  421.  
  422.  
  423.  
  424.  
  425.  
  426. OSErr YesNoDialog(char    *thePrompt,int    theDefault)
  427. {
  428. DialogPtr        theDPtr,tempDPtr;
  429. short            itemHit, type;
  430. Handle            theItem;
  431. Rect            tempRect;
  432. GrafPtr            oldPort;
  433. char            tempString[256];
  434. EventRecord        theEvent;
  435.  
  436.     GetPort(&oldPort);
  437.     
  438.     InitCursor();
  439.     
  440.     if (!(theDPtr = GetNewDialog(OKCANCELDLOG, nil,(WindowPtr) -1L)))
  441.     {
  442.         FatalError("Can't allocate memory for window");
  443.     }
  444.     
  445.     SetPort(theDPtr);
  446.     
  447.     CenterWindow(theDPtr);
  448.     ShowWindow(theDPtr);
  449.     
  450.     ((DialogPeek) theDPtr)->aDefItem = theDefault;
  451.     
  452.     GetDItem(theDPtr, 4, &type, &theItem, &tempRect);
  453.     SetDItem(theDPtr, 4, type,(Handle) HiliteDefaultButton, &tempRect);
  454.     
  455.     strcpy(tempString,thePrompt);
  456.     CtoPstr(tempString);
  457.     GetDItem(theDPtr, 3, &type, &theItem, &tempRect);
  458.     SetIText(theItem,(unsigned char *) tempString);
  459.     
  460.     if (GetNextEvent(updateMask,&theEvent))
  461.     {
  462.         if (theEvent.what == kHighLevelEvent)
  463.         {
  464.             DoHighLevel(&theEvent);
  465.         }
  466.  
  467.         else if (theEvent.message == (long) theDPtr)
  468.         {
  469.             DialogSelect(&theEvent,&tempDPtr,&itemHit);
  470.         }
  471.     }
  472.     
  473.     
  474.     do 
  475.     {
  476.         ModalDialog((ModalFilterProcPtr) MyFilter,&itemHit);
  477.         
  478.     } while (itemHit!=1 && itemHit!=2);
  479.     
  480.     DisposDialog(theDPtr);
  481.     SetPort(oldPort);
  482.     
  483.     return(itemHit);
  484. }